<template>
  <el-dialog :visible.sync="visible" title="派单" :close-on-click-modal="false" :close-on-press-escape="false"
    width="45%">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
      :label-width="$i18n.locale === 'en-US' ? '170px' : '140px'">
      <el-row :gutter="30">
        <el-col :span="24">
          <el-form-item label="电话" prop="complainantPhone">
            <el-input v-model="dataForm.complainantPhone" disabled></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="内容" prop="content">
            <el-input type="textarea" rows="3" v-model="dataForm.content" disabled>
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="图片" prop="pic" v-if="dataForm.pic">
            <el-image style="width: 150px; height: 150px" :src="dataForm.pic" fit="contain"
              :preview-src-list="[dataForm.pic]"></el-image>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="负责人" prop="dptId">
            <el-select v-model="dataForm.dptId" placeholder="请选择" clearable>
              <el-option v-for="item in dptList" :key="item.id" :label="item.name" :value="item.id">
              </el-option>
            </el-select>
            <el-select v-model="dataForm.head" placeholder="请选择" clearable style="margin-left:20px">
              <el-option v-for="item in headList" :key="item.id" :label="item.realName" :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{
        $t("confirm")
      }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
export default {
  data() {
    return {
      visible: false,
      headList: [],
      dptList: [],
      dataForm: {
        dptId: "",
        id: "",
        head: "",

      },
    };
  },
  computed: {
    dataRule() {
      return {
        head: [{
          required: true,
          message: this.$t("validate.required"),
          trigger: "blur",
        }],
        dptId: [{
          required: true,
          message: this.$t("validate.required"),
          trigger: "blur",
        }]

      };
    },
  },
  watch: {
    "dataForm.dptId"(newval, oldval) {
      if (newval && newval != oldval) {
        this.getHeadData(newval)
      } else {
        this.headList = [];
        this.dataForm.head = "";
      }
    }
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.getDptData();
          this.getInfo();
        }
      });
    },
    getDptData() {
      this.$http.get('/scenic/complaintrecommendation/list').then(res => {
        this.dptList = res.data.data || [];
      })
    },
    getHeadData(id) {
      this.$http.get('/scenic/complaintrecommendation/users/' + id).then(res => {
        this.headList = res.data.data || [];
      })
    },
    //获取信息
    getInfo() {
      this.$http
        .get("/scenic/complaintrecommendation/" + this.dataForm.id)
        .then(({
          data: res
        }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data,
          };

        })
        .catch(() => { });
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs["dataForm"].validate((valid) => {
          if (!valid) {
            return false;
          }
          console.log(this.dataForm)
          this.$http.post(
            "/scenic/complaintrecommendation/order",
            this.dataForm
          )
            .then(({
              data: res
            }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            })
            .catch(() => { });
        });
      },
      1000, {
      leading: true,
      trailing: false
    }
    ),
  },
};
</script>

<style lang="scss" scoped>
h3 {
  padding: 30px 0 15px;
  width: 94%;
  margin: 0 auto 30px;
  border-bottom: 1px solid #ddd;
  color: #303133;
  font-size: 22px;
  overflow: hidden;
}

.rooms .item {
  display: inline-block;
  margin-right: 15px;
  position: relative;

  .el-icon-error {
    position: absolute;
    right: 6px;
    top: -6px;
    font-size: 20px;
    color: #dd0606;
  }
}
</style>
